package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsMenu;
import pers.myz.entity.SnsMenu2;
import pers.myz.entity.SnsUser;
import pers.myz.service.SnsMenu2Service;
import pers.myz.service.SnsMenuService;
import pers.myz.service.SnsUserService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-user")
public class SnsUserController {

    @Autowired
    private SnsUserService snsUserService;
    @Autowired
    private SnsMenuService snsMenuService;
    @Autowired
    private SnsMenu2Service snsMenu2Service;

    @GetMapping("/list")
    public List<SnsUser> list(){
        return snsUserService.list();
    }
    @GetMapping("/findByAccount")
    public Result findByAccount(@RequestParam String account){
        List list = snsUserService.lambdaQuery().eq(SnsUser::getAccount,account).list();
        if(list.size()>0){
            SnsUser user1 = (SnsUser)list.get(0);
            HashMap res = new HashMap();
            res.put("user",user1);
            return Result.suc(res);
        }else{
            return Result.fail();
        }
    }

    @PostMapping("/save")
    public Result save(@RequestBody SnsUser user){
        return snsUserService.save(user)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody SnsUser user){
        return snsUserService.updateById(user)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return snsUserService.removeById(id)?Result.suc():Result.fail();
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody SnsUser user){
        List list = snsUserService.lambdaQuery()
                .eq(SnsUser::getAccount,user.getAccount())
                .eq(SnsUser::getPassword,user.getPassword()).list();


        if(list.size()>0){
            SnsUser user1 = (SnsUser)list.get(0);
            if(user1.getPermission()==1){

                List menuList = snsMenuService.lambdaQuery().like(SnsMenu::getMenuright,user1.getRoleId()).list();
                List menuList2 = snsMenu2Service.lambdaQuery().like(SnsMenu2::getMenuright,user1.getRoleId()).list();
                HashMap res = new HashMap();
                res.put("user",user1);
                res.put("menu",menuList);
                res.put("menu2",menuList2);
                return Result.suc(res);
            }else{
                return Result.noPermission();
            }
        }
        return Result.fail();
    }

    //列表、分页
    @PostMapping("/listPage")
    public Result listPageC1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String userName = (String)param.get("userName");
        String gender = (String)param.get("gender");
        String roleId = (String)param.get("roleId");

        Page<SnsUser> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<SnsUser> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(userName) && !"null".equals(userName)){
            lambdaQueryWrapper.like(SnsUser::getUserName,userName);
        }
        if(StringUtils.isNotBlank(gender)){
            lambdaQueryWrapper.eq(SnsUser::getGender,gender);
        }
        if(StringUtils.isNotBlank(roleId)){
            lambdaQueryWrapper.eq(SnsUser::getRoleId,roleId);
        }

        IPage result = snsUserService.pageC(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());

        return Result.suc(result.getRecords(),result.getTotal());
    }

}
//提交刷新用
